package com.jwell.panrank.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panrank.R
import com.jwell.panrank.application.App
import com.jwell.panrank.contract.ElectronicPoundContract
import com.jwell.panrank.entity.ElectronicePoundBean
import com.jwell.panrank.entity.MeasurePlace
import com.jwell.panrank.entity.UnLoadingPointBean
import com.jwell.panrank.model.ElectronicPoundModel
import com.jwell.panrank.presenter.ElectronicPoundPresenter
import com.jwell.panrank.tools.DataModel
import com.jwell.panrank.ui.adapter.ElectronicAdapter
import com.jwell.panrank.ui.custom.DateChooseWheelViewDialog
import com.jwell.panrank.ui.dialog.ChooseStringDialog
import kotlinx.android.synthetic.main.activity_electronic_pound_search.*
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 电子磅单搜索
 */
class ElectronicPoundSearchActivity : MVPBaseActivity<ElectronicPoundPresenter>(), ElectronicPoundContract.View, View.OnClickListener {

    private var haveSearch = false
    private var searchData = ElectronicPoundModel.RequestData()
    private var listData = ArrayList<ElectronicePoundBean>()
    private var adapter = ElectronicAdapter(listData)
    override fun createPresenter(): ElectronicPoundPresenter? {
        return ElectronicPoundPresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        setContentView(R.layout.activity_electronic_pound_search)
        initToolBar("磅单搜索", true, null)
        toolbar_right_text_save.text = "搜索"
        toolbar_right_text_save.visibility = View.VISIBLE
        setLabel1(true, false)
        setLabel2(true, false, false)
        setLabel3(false, false, false, false, false, false)
        setListener()
        initData()
    }

    override fun setSearchTotalCount(count: Int) {
        if (count > 0) {
            initToolBar("搜索到" + count + "条磅单", true, null)
        } else {
            initToolBar("未搜索到相关磅单", true, null)
        }
    }

    private fun initData() {
        initEmptyAndErrorView()
        loadingLayout.visibility = View.GONE
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "无相关电子磅单数据"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    private fun setListener() {
        toolbar_right_text_save.setOnClickListener(this)
        rl_item11.setOnClickListener(this)
        rl_item12.setOnClickListener(this)
        rl_item21.setOnClickListener(this)
        rl_item22.setOnClickListener(this)
        rl_item23.setOnClickListener(this)
        rl_item31.setOnClickListener(this)
        rl_item32.setOnClickListener(this)
        rl_item33.setOnClickListener(this)
        rl_item34.setOnClickListener(this)
        rl_item35.setOnClickListener(this)
        rl_item36.setOnClickListener(this)
        text_time1.setOnClickListener(this)
        text_time2.setOnClickListener(this)
        text_choose.setOnClickListener(this)
    }

    private fun setLabel1(c1: Boolean, c2: Boolean) {
        rl_item11.isSelected = c1
        rl_item12.isSelected = c2
        setTextColor(c1, text_label11, image_tick_11)
        setTextColor(c2, text_label12, image_tick_12)
    }

    private fun setLabel2(c1: Boolean, c2: Boolean, c3: Boolean) {
        rl_item21.isSelected = c1
        rl_item22.isSelected = c2
        rl_item23.isSelected = c3
        setTextColor(c1, text_label21, image_tick_21)
        setTextColor(c2, text_label22, image_tick_22)
        setTextColor(c3, text_label23, image_tick_23)
    }

    private fun setLabel3(c1: Boolean, c2: Boolean, c3: Boolean, c4: Boolean, c5: Boolean, c6: Boolean) {
        rl_item31.isSelected = c1
        rl_item32.isSelected = c2
        rl_item33.isSelected = c3
        rl_item34.isSelected = c4
        rl_item35.isSelected = c5
        rl_item36.isSelected = c6
        setTextColor(c1, text_label31, image_tick_31)
        setTextColor(c2, text_label32, image_tick_32)
        setTextColor(c3, text_label33, image_tick_33)
        setTextColor(c4, text_label34, image_tick_34)
        setTextColor(c5, text_label35, image_tick_35)
        setTextColor(c6, text_label36, image_tick_36)
    }

    private fun setTextColor(isChecked: Boolean, textView: TextView, imageView: ImageView) {
        when (isChecked) {
            true -> {
                textView.setTextColor(application.resources.getColor(R.color.black))
                imageView.visibility = View.VISIBLE
            }
            false -> {
                textView.setTextColor(application.resources.getColor(R.color.c999999))
                imageView.visibility = View.GONE
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.text_choose -> {//选择计料点
                var unLoadingPintList: List<MeasurePlace>? = null
                DataModel.getInstance().measurePlace?.let {
                    unLoadingPintList = it
                }
                var stringList = mutableListOf<String>()
                unLoadingPintList?.let {
                    it.mapTo(stringList) { it.caR_MEASURE_PLACE_DESCR }
                }
                if (stringList.isEmpty()) {
                    toast("未获取到计量点信息")
                    return
                }
                var dialog1 = ChooseStringDialog.Builder(this, stringList)
                        .setOnclickListener { text ->
                            edit_measure_address.setText(text)
                        }
                        .creat()
                dialog1.show()
            }
            R.id.text_time1 -> {//选择开始时间
                val startDateChooseDialog = DateChooseWheelViewDialog(this@ElectronicPoundSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time1.text = time

                })
                startDateChooseDialog.setDateDialogTitle("供量开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time2 -> {//选择结束时间
                val startDateChooseDialog = DateChooseWheelViewDialog(this@ElectronicPoundSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time2.text = time

                })
                startDateChooseDialog.setDateDialogTitle("供量结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.toolbar_right_text_save -> {
                searchData = ElectronicPoundModel.RequestData()
                when (toolbar_right_text_save.text) {
                    "重新搜索" -> {
                        list_layout.visibility = View.GONE
                        scrollView_search.visibility = View.VISIBLE
                        haveSearch = false
                    }
                    "搜索" -> {
                        if (rl_item11.isSelected) {
                            searchData.searchType = 1
                        }
                        if (rl_item12.isSelected) {
                            searchData.searchType = 2
                        }
                        if (rl_item21.isSelected) {
                            searchData.type = 3
                        }
                        if (rl_item22.isSelected) {
                            searchData.type = 1
                        }
                        if (rl_item23.isSelected) {
                            searchData.type = 2
                        }
                        if (rl_item31.isSelected) {
                            searchData.measurementType = 3
                        }
                        if (rl_item32.isSelected) {
                            searchData.measurementType = 1
                        }
                        if (rl_item33.isSelected) {
                            searchData.measurementType = 2
                        }
                        if (rl_item34.isSelected) {
                            searchData.measurementType = 4
                        }
                        if (rl_item35.isSelected) {
                            searchData.measurementType = 5
                        }
                        if (rl_item36.isSelected) {
                            searchData.measurementType = 6
                        }
                        searchData.supplierName = edit_providerName.text.toString()
                        searchData.forBillCode = edit_code.text.toString()
                        searchData.driverName = edit_driverName.text.toString()
                        searchData.carNum = edit_carNum.text.toString()
                        searchData.materialName = edit_goodsName.text.toString()
                        searchData.forwardingUnit = edit_sendCom.text.toString()
                        searchData.receivingUnit = edit_receiptCom.text.toString()
                        searchData.HEAVY_CAR_WEIGHT_ROOM = edit_roomNum.text.toString()
                        searchData.weightNo = edit_poundNum.text.toString()
                        searchData.carMeasurePlaceDescr = edit_measure_address.text.toString()
                        searchData.carMeasureByDescr = edit_measure_person.text.toString()
                        searchData.startDate = text_time1.text.toString()
                        searchData.endDate = text_time2.text.toString()
                        searchData.remark = edit_remark.text.toString()
                        searchData.skipCount = 0
                        showProgress("", true)
                        presenter?.searchListData(searchData)
                        haveSearch = true
                        list_layout.visibility = View.VISIBLE
                        scrollView_search.visibility = View.GONE
                    }
                }
                when (haveSearch) {
                    true -> {
                        toolbar_right_text_save.text = "重新搜索"
                    }
                    false -> {
                        toolbar_right_text_save.text = "搜索"
                        initToolBar("磅单搜索", true, null)
                    }
                }
            }
            R.id.rl_item11 -> setLabel1(true, false)
            R.id.rl_item12 -> setLabel1(false, true)
            R.id.rl_item21 -> setLabel2(true, false, false)
            R.id.rl_item22 -> setLabel2(false, true, false)
            R.id.rl_item23 -> setLabel2(false, false, true)
            R.id.rl_item31 -> setLabel3(true, false, false, false, false, false)
            R.id.rl_item32 -> setLabel3(false, true, false, false, false, false)
            R.id.rl_item33 -> setLabel3(false, false, true, false, false, false)
            R.id.rl_item34 -> setLabel3(false, false, false, true, false, false)
            R.id.rl_item35 -> setLabel3(false, false, false, false, true, false)
            R.id.rl_item36 -> setLabel3(false, false, false, false, false, true)

        }
    }

    override fun showListData(data: List<ElectronicePoundBean>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < 10) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }

}
